﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NFramework.DataAccess;
using NFramework.DataAccess.Querying;

namespace NFramework.UnitTests.DataAccessTests.QueryingTests
{
    [TestClass]
    public class TableQueryTests : Global
    {

        [ClassInitialize]
        public static void RefreshTable(TestContext testContext)
        {
            CallEmbededResourceOnDatabase("NFramework.UnitTests.DataAccessTests.QueryingTests.TableQueryTests.sql");
        }

        [TestMethod]
        public void GetDataFromDatabaseHasCorrentNumberOfRows()
        {
            using (IReaderHelper rh = ReaderHelper.Create(new TableQuery("TableQueryTests")))
            {
                int i = 0;
                while (rh.Read()) { i++; }
                Assert.AreEqual(10, i);
            }
        }

        [TestMethod]
        public void DatabaseGetLinesUpWithStringGet()
        {
            using (IReaderHelper rhSimple = ReaderHelper.Create("SELECT * FROM TableQueryTests"))
            {
                using (IReaderHelper rhTable = ReaderHelper.Create(new TableQuery("TableQueryTests")))
                {
                    while (rhSimple.Read() && rhTable.Read())
                    {
                        for (int i = 0; i < rhSimple.FieldCount; ++i)
                        {
                            Assert.AreEqual(rhSimple.GetValue(i), rhTable.GetValue(i));
                        }
                    }
                    Assert.IsFalse(rhSimple.Read());
                    Assert.IsFalse(rhTable.Read());
                }
            }
        }

        public class Student
        {
            public string LastName { get; set; }
            public List<int> Scores { get; set; }
        }


        [TestMethod]
        public void TryOutLinq()
        {
            var context = new DataClasses1DataContext();
            var source = context.TableQueryTests;

            var result = from q in source
                             where q.Id > 5
                             select q.Value;

            //var result = from q in TableQueryTest where q.Id > 5 select q;

            // Use a collection initializer to create the data source. Note that 
            // each element in the list contains an inner sequence of scores.
            List<Student> students = new List<Student>
        {
           new Student {LastName="Omelchenko", Scores= new List<int> {97, 72, 81, 60}},
           new Student {LastName="O'Donnell", Scores= new List<int> {75, 84, 91, 39}},
           new Student {LastName="Mortensen", Scores= new List<int> {88, 94, 65, 85}},
           new Student {LastName="Garcia", Scores= new List<int> {97, 89, 85, 82}},
           new Student {LastName="Beebe", Scores= new List<int> {35, 72, 91, 70}} 
        };

            // Use a compound from to access the inner sequence within each element.
            // Note the similarity to a nested foreach statement.
            var scoreQuery = from student in students
                             from score in student.Scores
                             where score > 90
                             select new { Last = student.LastName, score };

            // Execute the queries.
            Console.WriteLine("scoreQuery:");
            foreach (var student in scoreQuery)
            {
                Console.WriteLine("{0} Score: {1}", student.Last, student.score);
            }



        }
    }
}
